home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 March / EnigmA AMIGA RUN 05 (1996)(G.R. Edizioni)(IT)[!][issue 1996-03][Skylink CD IV].iso / earcd / comm2 / alist.lha / src / aladdr.e < prev    next >
Text File  |  1995-11-08  |  3KB  |  142 lines

  1. /* ALAddr.M */
  2.  
  3. OPT MODULE
  4. OPT EXPORT
  5.  
  6. /* Parses the addr lines for name/addr */
  7.  
  8. /*
  9.  * Returns name,address
  10.  *
  11.  * Both must be allocated with String()
  12.  *
  13.  * Formulae is to find the first word with @ in it and extract it as the address, using the
  14.  *   remaining string as the name.
  15.  */
  16. PROC parse_addr (str:PTR TO CHAR)
  17.    DEF nam:PTR TO CHAR, addr:PTR TO CHAR, tmp:PTR TO CHAR, x, y, len
  18.  
  19.    len := StrLen (str)
  20.    x := InStr (str, '@')
  21.    IF (x > -1)
  22.       /* Found it! */
  23.       IF (IF (x > 0)  THEN (invalid (str[x-1])) ELSE FALSE) OR (invalid (str[x+1]))
  24.          /* Nope, just an @ by itself */
  25.          nam,addr := parse_again (str, x)
  26.          RETURN nam,addr
  27.       ENDIF
  28.  
  29.       y := x
  30.       WHILE ((x > 0) AND (invalid (str[x]) = FALSE))
  31.          DEC x
  32.       ENDWHILE
  33.       IF (invalid (str[x]))  THEN INC x
  34.       /* Found the start, now find the end */
  35.       WHILE (invalid (str[y]) = FALSE)
  36.          INC y
  37.       ENDWHILE
  38.       IF (invalid (str[y]))  THEN DEC y
  39.  
  40.       addr := String (y-x+1)
  41.       StrCopy (addr, str+x, y-x+1)
  42.       IF (len > (y-x+1))
  43.          nam := String (len - y + x - 1)
  44.          IF (x)  THEN StrCopy (nam, str, x)
  45.          IF (y < (len-1))  THEN StrAdd (nam, str+y+1)
  46.       ELSE
  47.          nam := String (1)
  48.       ENDIF
  49.       RETURN nam,addr
  50.    ENDIF
  51.  
  52.    /* We have no @ (SMTP address) in the string. */
  53.    x := InStr (str, '!')
  54.    IF (x > -1)
  55.       /* Found it! */
  56.  
  57.       IF (IF (x > 0)  THEN (invalid (str[x-1])) ELSE FALSE) OR (invalid (str[x+1]))
  58.          nam,addr := parse_again (str, x)
  59.          RETURN nam,addr
  60.       ENDIF
  61.  
  62.       y := x
  63.       WHILE ((x > 0) AND (invalid (str[x]) = FALSE))
  64.          DEC x
  65.       ENDWHILE
  66.       IF (invalid (str[x]))  THEN INC x
  67.       /* Found the start, now find the end */
  68.       WHILE (invalid (str[y]) = FALSE)
  69.          INC y
  70.       ENDWHILE
  71.       IF (invalid (str[y]))  THEN DEC y
  72.  
  73.       addr := String (y-x+1)
  74.       StrCopy (addr, str+x, y-x+1)
  75.       IF (len > (y-x+1))
  76.          nam := String (len - y + x - 1)
  77.          IF (x)  THEN StrCopy (nam, str, x)
  78.          IF (y < (len-1))  THEN StrAdd (nam, str+y+1)
  79.       ELSE
  80.          nam := String (1)
  81.       ENDIF
  82.       RETURN nam,addr
  83.    ENDIF
  84.  
  85.    /* We have no ! (UUCP address) in the string */
  86.    /* Well, I know of no other easy way to extrapolate an address. */
  87.    /* So just use the first word, and pray.  (: */
  88.    x := 0
  89.    WHILE (invalid (str[x]))
  90.       INC x
  91.    ENDWHILE
  92.    y := x
  93.    WHILE (invalid (str[y]) = FALSE)
  94.       INC y
  95.    ENDWHILE
  96.  
  97.    IF (y > x)
  98.       addr := String (y-x)
  99.       StrCopy (addr, str+x, y-x)
  100.    ELSE
  101.       /* Well, can't find even a single word! */
  102.       addr := String (1)
  103.    ENDIF
  104.    IF (y < len)
  105.       nam := String (len - y)
  106.       StrCopy (nam, str+y)
  107.    ELSE
  108.       nam := String (1)
  109.    ENDIF
  110. ENDPROC nam,addr
  111.  
  112.  
  113. /*
  114.  * Used only by parse_addr().  Sort of a private function.
  115.  * I'm using a funciton because it's called from a few places.
  116.  */
  117. PROC parse_again (str:PTR TO CHAR, x)
  118.    DEF nam, addr, tmp
  119.  
  120.    INC x
  121.    nam,addr := parse_addr (str + x)
  122.    tmp := nam
  123.    nam := String (StrLen (tmp) + x)
  124.    StrCopy (nam, str, x)
  125.    StrAdd (nam, tmp)
  126.    DisposeLink (tmp)
  127. ENDPROC nam,addr
  128.  
  129.  
  130. /*
  131.  * Checks to see if a character is one which can't appear in an address.
  132.  */
  133. PROC invalid (ch)
  134.    SELECT 256 OF ch
  135.    CASE 0 TO 32, "<", ">", "(", ")", "[", "]", 127 TO 255
  136.       RETURN TRUE
  137.    DEFAULT
  138.       RETURN FALSE
  139.    ENDSELECT
  140. ENDPROC
  141.  
  142.